var OfficialInfoDlg = {
    OfficialInfoData : {},
    zTreeInstance : null,
    validateFields: {
        OfficialName: {
            validators: {
                notEmpty: {
                    message: '关键字名称不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '关键字名称长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
    }
};

/**
 * 清除数据
 */
OfficialInfoDlg.clearData = function() {
    this.OfficialInfoData = {};
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
OfficialInfoDlg.set = function(key, val) {
    this.OfficialInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
OfficialInfoDlg.get = function(key) {
    return $("#" + key).val();
};

/**
 * 关闭此对话框
 */
OfficialInfoDlg.close = function() {
    parent.layer.close(window.parent.Official.layerIndex);
};

/**
 * 点击部门ztree列表的选项时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
OfficialInfoDlg.onClickOfficial = function(e, treeId, treeNode) {
    $("#pName").attr("value", OfficialInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
};

OfficialInfoDlg.updatePushContent = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/official/updatePushContent", function(data){
        Feng.success("修改成功!");
        window.parent.Official.table.refresh();
        OfficialInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.OfficialInfoData);
    ajax.start();
};

OfficialInfoDlg.sendingContent = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var openId = document.getElementById("openId").value;
    if (openId == null || openId == "") {
        alert("微信号唯一标识不能为空！");
        return;
    }
    var sendingContent = document.getElementById("sendingContent").value;
    if (sendingContent == null || sendingContent == "") {
        alert("微信公众号单发消息不能为空！");
        return;
    }
    if (confirm("确定要单发此内容？?")) {
        var ajax = new $ax(Feng.ctxPath + "/official/sendingContent", function (data) {
            Feng.success("单发成功!");
            window.parent.Official.table.refresh();
            OfficialInfoDlg.close();
        }, function (data) {
            Feng.error("单发失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.OfficialInfoData);
        ajax.start();
    }
};

OfficialInfoDlg.sendingContents = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var sendingContent = document.getElementById("sendingContent").value;
    if (sendingContent == null || sendingContent == "") {
        alert("微信公众号群发消息不能为空！");
        return;
    }
    if (confirm("确定要群发此内容？?")) {
        var ajax = new $ax(Feng.ctxPath + "/official/sendingContents", function (data) {
            Feng.success("群发成功!");
            window.parent.Official.table.refresh();
            OfficialInfoDlg.close();
        }, function (data) {
            Feng.error("群发失败!" + data.responseJSON.message + "!");
        });
        ajax.set(this.OfficialInfoData);
        ajax.start();
    }
};

/**
 * 收集数据
 */
OfficialInfoDlg.collectData = function() {
    this.set('id').set('keyword').set('type').set('content').set('pushContent').set('openId').set('sendingContent');
};

/**
 * 验证数据是否为空
 */
OfficialInfoDlg.validate = function () {
    $('#OfficialInfoForm').data("bootstrapValidator").resetForm();
    $('#OfficialInfoForm').bootstrapValidator('validate');
    return $("#OfficialInfoForm").data('bootstrapValidator').isValid();
};

/**
 * 提交添加
 */
OfficialInfoDlg.addSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/official/add", function(data){
        if(data==1){
            alert("该关键字已经存在！")
        }else{
            Feng.success("添加成功!");
            window.parent.Official.table.refresh();
            OfficialInfoDlg.close();
        }
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.OfficialInfoData);
    ajax.start();
};

/**
 * 提交修改
 */
OfficialInfoDlg.editSubmit = function() {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var ajax = new $ax(Feng.ctxPath + "/official/update", function(data){
        Feng.success("修改成功!");
        window.parent.Official.table.refresh();
        OfficialInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.OfficialInfoData);
    ajax.start();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentOfficialMenu" || $(
            event.target).parents("#parentOfficialMenu").length > 0)) {
        OfficialInfoDlg.hideOfficialSelectTree();
    }
}

$(function() {
    Feng.initValidator("OfficialInfoForm", OfficialInfoDlg.validateFields);
});
